const util = require('../../utils/util')
const regeneratorRuntime = require('../../utils/runtime')

//获取应用实例
const app = getApp()

Page({
  data: {
    initNavList: [],
    navList: []
  },
  // onLoad: function(options) {
  // wx.setStorageSync('route', 'init')
  // if (options.re) {
  //   wx.navigateTo({
  //     url: `${options.re}?${options.params.replace(/-/g, '&').replace(/:/g, '=')}`,
  //   })
  // }
  // },
  onLoad: function() {
    const _this = this
    if (!wx.getStorageSync('token')) {
      // 登录
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          wx.request({
            url: app.globalData.requestUrl + '/api/home/login/?code=' + res.code,
            data: {},
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res) {
              if (res.data.code === 1) {
                wx.setStorageSync('token', res.data.data.token)
                if (res.data.data.state == 1) {
                  wx.navigateTo({
                    url: '/pages/login/login',
                  })
                }
                _this.getMenu()
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      })
    } else {
      this.getMenu()
    }
  },
  onShow: function() {
    if (wx.getStorageSync('init') == 'true' && wx.getStorageSync('token')) {
      util.WXRequest('/api/user/homeLog').then(res => {})
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},
  toLink: function(e) {
    wx.navigateTo({
      url: '/pages/list/list?cate_id=' +
        e.currentTarget.dataset.cateid +
        '&catename=' +
        e.currentTarget.dataset.catename
    })
  },
  getMenu: function() {
    const _this = this
    util.WXRequest('/api/user/getBanner').then(res => {
      if (res.code === 1) {
        const cb = function() {
          let initNavList = _this.data.initNavList
          let currentNavList = res.data.map((item, index) => {
            initNavList[index] = {
              id: res.data[index]['id'],
              name: res.data[index]['title'],
              color: res.data[index]['color']
            }
            return initNavList[index]
          })
          _this.setData({
            navList: currentNavList
          })
        }
        cb()
        if (wx.getStorageSync('init') != 'true' && !(wx.getLaunchOptionsSync().scene == 1007 || wx.getLaunchOptionsSync().scene == 1008)) {
          wx.setStorageSync('init', 'true')
          _this.getLastLog()
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  getLastLog: function() {
    util.WXRequest('/api/user/lastLog').then(res => {
      if (res.code === 1) {
        if (res.data.cate_id == 0) {
          return
        }
        wx.navigateTo({
          url: '/pages/list/list?cate_id=' +
            res.data.cate_id +
            '&catename=' +
            res.data.get_cate.title,
        })
      }
    })
  }
})